const tree = {
  name: 'root',
  task: () => {
    console.log('root')
  },
  children: [
    {
      name: '一级子树 01',
      task: () => {
        console.log('一级子树 01')
      },
      children: [
        {
          name: '二级子树 01_01',
          task: () => {
            console.log('二级子树 01_01')
          },
          children: [],
        },
        {
          name: '二级子树 01_02',
          task: () => {
            console.log('二级子树 01_02')
          },
          children: [],
        },
      ],
    },
    {
      name: '一级子树 02',
      task: () => {
        console.log('一级子树 02')
      },
      children: [],
    },
  ],
}
const useIdeFiber = (tree) => {
  let nextFiber = tree
  const runFiber = (fiber) => {
    fiber.task()
    let preChild = null
    fiber.children.forEach((child, index) => {
      child.parent = fiber
      if (index === 0) {
        fiber.nextFiber = child
      } else {
        preChild.sibling = child
      }
      preChild = child
    })
    if (fiber.nextFiber) {
      return fiber.nextFiber
    }
    if (fiber.sibling) {
      return fiber.sibling
    }
    return fiber.parent.sibling ? fiber.parent.sibling : ''
  }
  const callBack = (IdleDeadline) => {
    while (IdleDeadline.timeRemaining() > 1 && nextFiber) {
      nextFiber = runFiber(nextFiber)
    }
    nextFiber && requestIdleCallback(callBack)
  }
  requestIdleCallback(callBack)
}
useIdeFiber(tree)
